package ${package.Controller};

import org.springframework.web.bind.annotation.RequestMapping;
<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>
import org.apache.commons.lang3.StringUtils;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.StrSpliter;
import com.yunc.common.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ${package.Entity}.${entity};
import ${package.Service}.${table.serviceName};

import java.util.List;

/**
 * ${table.comment!} 前端控制器
 *
 * @author ${author}
 * @since ${date}
 */
<#if restControllerStyle>
@RestController
<#else>
@Controller
</#if>
@RequestMapping("<#if package.ModuleName??>/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
<#else>
public class ${table.controllerName} {
</#if>

    @Autowired
    private ${table.serviceName} ${cfg.entityName}Service;

    @GetMapping("/page")
    public R page(Page<${table.entityName}> page, ${table.entityName} ${cfg.entityName}){
        ${cfg.entityName}Service.page(page, new QueryWrapper<>(${cfg.entityName}));
        return R.ok().put("data", page.getRecords()).put("count", page.getTotal());
    }

    @GetMapping("/info/{id}")
    public R info(@PathVariable Long id){
        ${table.entityName} ${cfg.entityName} = ${cfg.entityName}Service.getById(id);
        return R.ok().put("${cfg.entityName}", ${cfg.entityName});
    }

    @PostMapping("/save")
    public R save(@RequestBody ${table.entityName} ${cfg.entityName}){
        boolean flag = ${cfg.entityName}Service.save(${cfg.entityName});
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    @PutMapping(value = "/update")
    public R update(@RequestBody ${table.entityName} ${cfg.entityName}){
        boolean flag = ${cfg.entityName}Service.updateById(${cfg.entityName});
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    @DeleteMapping("/delete/{ids}")
    public R delete(@PathVariable String ids){
        if (StringUtils.isEmpty(ids)) {
            return R.error("传入值为空");
        }
        List<String> idsList = StrSpliter.split(ids, ',', 0, true, true);
        if (CollUtil.isEmpty(idsList)) {
            return R.error("传入值格式错误");
        }
        boolean flag = ${cfg.entityName}Service.removeByIds(idsList);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }
}
</#if>
